﻿
@{
    ViewBag.Title = "产品表";
    Layout = null;
}
<html>
<head>
    @Scripts.Render("~/bundles/easyui_scripts")
    @Styles.Render("~/bundles/easyui_style")
</head>
<body style="padding: 0;">
    <table id="dg" class="easyui-datagrid" style="width:100%;height:100%"
           url="/Home/产品表DataGrid"
           toolbar="#toolbar" pagination="true"
           rownumbers="true" fitColumns="true" singleSelect="true"pageSize="20">
        <thead>
            <tr>
                <th field="产品编号" width="50">产品编号</th>
                <th field="产品名称" width="50">产品名称</th>
                <th field="库存数量" width="50">库存数量</th>
                <th field="存储单位" width="50">存储单位</th>
                <th field="类型" width="50">类型</th>
                <th field="价格" width="50">价格</th>
            </tr>
        </thead>
    </table>
    <div id="toolbar">
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newUser()">新建产品单</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editUser()">编辑产品单</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="destroyUser()">删除产品单</a>
    </div>

    <div id="dlg" class="easyui-dialog" style="width:400px" data-options="closed:true,modal:true,border:'thin',buttons:'#dlg-buttons'">
        <form id="fm" method="post" novalidate style="margin:0;padding:20px 50px;font-size:12px">
            <h3>产品单</h3>
            <div style="margin-bottom:10px">
                <input id="产品编号" name="产品编号" class="easyui-textbox" required="true" label="产品编号:" id="产品编号" style="width:100%">
            </div>
            <div style="margin-bottom:10px">
                <input name="产品名称" class="easyui-textbox" required="true" label="产品名称:" style="width:100%">
            </div>
            <div style="margin-bottom:10px">
                <input name="存储单位" class="easyui-combobox" required="true" label="存储单位:" style="width:100%" data-options="
		            valueField: 'label',
		            textField: 'value',
		            data: [{
			            label: '个',
			            value: '个'
		            },{
			            label: '件',
			            value: '件'
		            }]">
            </div>
            <div style="margin-bottom:10px">
                <input name="类型" class="easyui-combobox" required="true" label="类型:" style="width:100%" data-options="
		            valueField: 'label',
		            textField: 'value',
		            data: [{
			            label: '通讯',
			            value: '通讯'
		            },{
			            label: '显示',
			            value: '显示'
		            }]">
            </div>
            <div style="margin-bottom:10px">
                <input name="价格"class="easyui-textbox" required="true" label="价格():" style="width:100%">
            </div>
        </form>
    </div>
    <div id="dlg-buttons">
        <a href="javascript:void(0)" class="easyui-linkbutton c6" iconCls="icon-ok" onclick="saveUser()" style="width:90px">保存</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')" style="width:90px">关闭</a>
    </div>
</body>
</html>
<script type="text/javascript">
    // 页面加载函数，进行页面保护
    $(function () {
        var name = sessionStorage.getItem('name');
        if (name == null) {
            window.location = ("/Home/登录");
        } else {
            person = name; // 把person设为全局变量
        }
    });
    var url;
    function newUser() {
        $('#dlg').dialog('open').dialog('center').dialog('setTitle', '新建采购单');
        $('#fm').form('clear');
        url = "/Home/产品表Insert";
    }
    function editUser() {
        var row = $('#dg').datagrid('getSelected');
        if (row) {
            $('#dlg').dialog('open').dialog('center').dialog('setTitle', '编辑采购单');
            $('#fm').form('load', row);
            url = '/Home/产品表UpDate';
        }
    }
    //当我点击保存时，我应该确定是修改内容还是提交内容 编辑用户时传入一个值告诉他是要更新 当我删除时传入一个值告诉他是要删除 我没有这么做啊！！
    function saveUser() {
       var id = $('#产品编号').textbox('getValue')
        $.ajax({
            type: 'post',
            url: '/Home/产品表DataJson',
            data: {},
            success: function (data) {
                for (var i = 0; i < data.length; i++) {
                    if (id == data[i].产品编号) {
                        alert('重复产品编号!!');
                        $('#dlg').dialog('close');
                        return;
                    }
                }
            }
        });


        $('#fm').form('submit', {
            url: url,
            onSubmit: function () {
                return $(this).form('validate');
            },
            success: function () {
                $('#dlg').dialog('close');		// close the dialog
                $('#dg').datagrid('reload');	// reload the user data
            }
        });
    }
    function destroyUser() {
        var row = $('#dg').datagrid('getSelected');
        if (row) {
            $.messager.confirm('确认', '是否删除这条记录?', function (r) {
                if (r) {
                    $.post('/Home/产品表Delete', { id: row.产品编号 }, function (result) {
                        if (result) {
                            $('#dg').datagrid('reload');	// reload the user data
                        }
                    });
                }
            });
        }
    }
</script>
